/**
* Created by wust on 2019/5/14.
*/
<template xmlns:v-data-dictionary-directive="http://www.w3.org/1999/xhtml">
  <el-form ref="addModel" :model="addModel" :rules="rules" label-width="130px" style="width: 70%;" @submit.native.prevent>
    <el-form-item label="登录账号" prop="loginName">
      <el-input v-model="addModel.loginName" class="input-with-select">
        <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
      </el-input>
    </el-form-item>
    <el-form-item label="真实姓名" prop="realName">
      <el-input v-model="addModel.realName"></el-input>
    </el-form-item>
    <el-form-item label="邮箱">
      <el-input v-model="addModel.email"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <select class="form-control" v-data-dictionary-directive:A1003  v-model="addModel.sex">
      </select>
    </el-form-item>
    <el-form-item label="移动电话" prop="mobilePhone">
      <el-input v-model="addModel.mobilePhone"></el-input>
    </el-form-item>
    <el-form-item label="员工账号类型" prop="type">
      <select class="form-control" v-data-dictionary-directive:A1004 v-model="addModel.type">
      </select>
    </el-form-item>
    <el-form-item style="text-align: left;">
      <el-button type="primary" @click="onSubmit('addModel')" :disabled="submitButtonDisabled">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import Vue from 'vue'

export default {
  name: 'user-invite',
  data () {
    return {
      submitButtonDisabled: true,
      addModel: {
        loginName: '',
        realName: '',
        sex: '',
        email: '',
        type: '',
        mobilePhone: '',
        status: ''
      },
      rules: {
        loginName: [{required: true, message: '请输入登录账号', trigger: 'blur'}],
        realName: [{required: true, message: '请输入真实姓名', trigger: 'blur'}],
        sex: [{required: true, message: '请输选择性别', trigger: 'blur'}],
        mobilePhone: [{required: true, message: '请输入手机号码', trigger: 'blur'}],
        type: [{required: true, message: '请输选择用户类型', trigger: 'blur'}]
      }
    }
  },
  methods: {
    search: function () {
      if (Vue.$isNullOrIsBlankOrIsUndefined(this.addModel.loginName)) {
        this.$message({
          message: '请输入登录账号',
          type: 'warning'
        })
        return
      }

      Vue.$ajax({
        method: 'post',
        url: Vue.$adminServerURL + '/web/v1/UserController/findByLoginName?loginName=' + this.addModel.loginName
      }).then(res => {
        if (res.data.flag !== 'SUCCESS') {
          this.$message({
            message: res.data.message,
            type: 'warning'
          })
          this.submitButtonDisabled = true
        } else {
          this.$message({
            message: '查询成功',
            type: 'success'
          })
          this.addModel.realName = res.data.obj.accountName
          this.submitButtonDisabled = false
        }
      })
    },
    onSubmit: function (formData) {
      this.$refs[formData].validate((valid) => {
        if (!valid) {
        } else {
          Vue.$ajax({
            method: 'post',
            url: Vue.$adminServerURL + '/web/v1/UserController/inviteUser',
            data: this.addModel
          }).then(res => {
            if (res.data.flag !== 'SUCCESS') {
              if (!Vue.$isNullOrIsBlankOrIsUndefined(res.data.message)) {
                this.$message({
                  message: res.data.message,
                  type: 'warning'
                })
              }
            } else {
              this.$message({
                message: res.data.message,
                type: 'success'
              })
              this.resetForm('addModel')
            }
          })
        }
      })
    },
    resetForm (formData) {
      this.$refs[formData].resetFields()
    }
  }
}
</script>
